package com.kygeng.common.util;

import com.kygeng.common.model.document.CompanyInfoDoc;
import com.kygeng.common.model.CompanyDetail;

import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Title: CompanyIndexUtil
 * @Author: kygeng
 * @Package: com.kygeng.util
 * @Date: 2024/9/8 18:04
 * @Description: 公司索引工具类
 */
public class CompanyIndexUtil {

    /**
     * 将 CompanyDetail 对象转化为 CompanyInfoDoc
     *
     * @param companyDetailList
     * @return
     */
    public static List<CompanyInfoDoc> companyDetailToDoc(List<CompanyDetail> companyDetailList) {

        List<CompanyInfoDoc> result = new ArrayList<>();

        for (CompanyDetail detail : companyDetailList) {
            CompanyInfoDoc doc = new CompanyInfoDoc();
            BeanUtils.copyProperties(detail, doc);
            // 处理Location对象
            if (detail.getLocation() != null) {
                doc.setLocation(new GeoPoint(detail.getLocation().getLat(), detail.getLocation().getLon()));
            }
            doc.setRisk(splitList(detail.getRisk()));
            doc.setTags(splitList(detail.getTags()));
            result.add(doc);
        }

        return result;
    }

    private static List<String> splitList(List<String> strList) {
        List<String> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(strList)) {
            for (String r : strList) {
                result.addAll(Arrays.asList(r.split(",")));
            }
        }
        return result;
    }


}
